Imports System.Data.SqlClient

Public Class <Database>DataClass

    Public Shared Function GetConnection() As SqlConnection
        Dim connectionString As String _
            = "<ConnectionString>"
        Return New SqlConnection(connectionString)
    End Function

    Public Function getIdent_Current(ByVal Table As String) As Integer
        Dim query As String
        Dim connection As New SqlConnection
        Dim command As New SqlCommand
        Dim reader As SqlDataReader
        Dim returnValue As Integer = 0

        query = "SELECT IDENT_CURRENT('" & Table & "')"
        connection = GetConnection()
        command = New SqlCommand(query, connection)
        command.CommandType = CommandType.Text
        Try
            connection.Open()
            reader = command.ExecuteReader()
            If reader.HasRows = True Then
                While reader.Read()
                    returnValue = System.Convert.ToInt32(reader.GetValue(0))
                End While
            End If
            reader.Close()
            connection.Close()
        Catch ex As SqlException
            'MessageBox.Show(ex.Message, ex.GetType.ToString)
        Finally
            connection.Close()
        End Try
        Return returnValue
    End Function

    Public Function getIdent_Incr(ByVal Table As String) As Integer
        Dim query As String
        Dim connection As New SqlConnection
        Dim command As New SqlCommand
        Dim reader As SqlDataReader
        Dim returnValue As Integer = 0

        query = "SELECT IDENT_INCR('" & Table & "')"
        connection = GetConnection()
        command = New SqlCommand(query, connection)
        command.CommandType = CommandType.Text
        Try
            connection.Open()
            reader = command.ExecuteReader()
            If reader.HasRows = True Then
                While reader.Read()
                    returnValue = System.Convert.ToInt32(reader.GetValue(0))
                End While
            End If
            reader.Close()
            connection.Close()
        Catch ex As SqlException
            'MessageBox.Show(ex.Message, ex.GetType.ToString)
        Finally
            connection.Close()
        End Try
        Return returnValue
    End Function

    Public Function getAutoID(ByVal Mode As String, ByVal Table As String) As Integer
        Dim Ident_Current As Integer = getIdent_Current(Table)
        If Mode = "Last" Then
            Return Ident_Current
        ElseIf Mode = "New" Then
            Return Ident_Current + getIdent_Incr(Table)
        End If
        Return Nothing
    End Function

End Class

